<template>
	<view class="">
		<view class="m-2 p-3 bg-white rounded-20 d-flex j-sb">
			<view class="">设置库存</view>
			<view class="">
				<input type="digit" :focus="true" v-model="price" @input="price_input" class="text-right" placeholder="请输入库存" />
			</view>
		</view>
		
		<view class="footer"></view>
		<view class="bg-white release py-2 w-100 position-fixed bottom-0">
			<button type="warn" @click="preserve">保存</button>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				e:"", // 参数
				price:"", // 价格
				goods_id:0, // 商品ID
				products:JSON.parse(uni.getStorageSync("products"))
			}
		},
		methods:{
			// 本店售价
			price_input(e){
				console.log(e)
			},
			// 保存
			preserve(){
				if(this.e=="本店"){
					let products=this.products.reduce((acc,item)=>{
						acc.push({
							product_id:item.product_id,
							price:this.price
						})
						return acc
					},[])
					this.$H.post("v2/store/goods/product/price/update",{
						goods_id:this.goods_id,
						products:JSON.stringify(products)
					}).then(res=>{
						console.log(res)
						this.$u.toast('修改成功');
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					})
				}else if(this.e=="成本价"){
					let products=this.products.reduce((acc,item)=>{
						acc.push({
							product_id:item.product_id,
							cost_price:this.price
						})
						return acc
					},[])
					this.$H.post("v2/store/goods/product/costprice/update",{
						goods_id:this.goods_id,
						products:JSON.stringify(products)
					}).then(res=>{
						console.log(res)
						this.$u.toast('修改成功');
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					})
				}else if(this.e=="库存"){
					let products=this.products.reduce((acc,item)=>{
						acc.push({
							product_id:item.product_id,
							number:this.price
						})
						return acc
					},[])
					this.$H.post("v2/store/goods/product/stock/update",{
						goods_id:this.goods_id,
						products:JSON.stringify(products)
					}).then(res=>{
						console.log(res)
						this.$u.toast('修改成功');
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					})
				}
			}
		},
		onUnload() {
			uni.removeStorageSync("products")
		},
		onLoad(e) {
			this.e=e.e
			this.goods_id=e.goods_id
			console.log(e)
		}
	}
</script>

<style>
</style>
